/*******************************************************************************
 * Copyright (c) 2012, G. Weirich and Elexis
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *    G. Weirich - initial implementation
 *******************************************************************************/

package ch.elexis.easy.developer.resources.model;

import ch.elexis.core.admin.AccessControlDefaults;
import ch.elexis.core.admin.InsufficientRightsException;
import ch.elexis.core.data.Attribute;
import ch.elexis.core.data.IAttribute;
import ch.elexis.core.data.IAttribute.type;
import ch.elexis.persistence.sql.model.PersistentObject;

/**
 * This is an arbitrary sample data type. It has a name and a value as its attributes. Note: If we
 * want to make this data type available to other parts of elexis, we must provide a
 * PersistentObjectFactory and register that factory at the Extension Point
 * ch.elexis.core.PersistentReference.
 * 
 * @see PersistentObjectFactory
 * 
 * @author gerry
 * 
 */
public class SampleData extends PersistentObject {
	/**
	 * Attributes declared this way are persisted transparently by the framework. They must be
	 * declared as static members of the type IAttribute (not Attribute) The default constructor
	 * takes only a name. This will create a indexable text field (implementation depends on the
	 * type of storage selected)
	 * 
	 */
	public static final IAttribute FLD_NAME = new Attribute("name");
	/**
	 * This Constructor takes access rights and Type information as parameters
	 */
	public static final IAttribute FLD_VALUE = new Attribute("value",
		AccessControlDefaults.EVERYBODY, AccessControlDefaults.EVERYBODY, type.INTEGRAL);
	
	/**
	 * A type derived from PersistentObject must implement at least this method
	 */
	@Override
	public String getLabel() throws InsufficientRightsException{
		Integer val = (Integer) getAttribute(FLD_VALUE.t()).getValue();
		if(val==null){
			val=0;
		}
		String name=checkNull(get(FLD_NAME.t()));
		return name + ", " + Integer.toString(val);
	}
	
	public SampleData(){}
	public SampleData(String id){
		super(id);
	}
}
